home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <stdio.h>
- #include <libc.h>
- #include <string.h>
-
- #include "options.h"
-
- option * option::head = 0;
- char option::optString[256];
-
- char * appName;
-
- commandArgument * theCommandArgument;
-
-
- option::option(int o, char * d)
- {
- opt = o;
- description = d;
- given = 0;
-
- if (o) { // if its a real option
- char buff[2]; // append to getopt string
- buff[0] = o;
- buff[1] = 0;
- strcat(optString, buff);
-
- next = head; // add self to list of options
- head = this;
- }
- }
-
- int
- option::wasGiven()
- {
- return given;
- }
-
- void
- option::usage()
- {
- fprintf(stderr, " -%c %s\n", opt, description);
- }
-
- int
- option::checkOption(int o, char *)
- {
- if (o == opt)
- given = 1;
-
- return (o == opt);
- }
-
-
- stringOption::stringOption(int o, char * d, char * defaultIn)
- : option(o, d)
- {
- arg = defaultIn;
-
- char buff[2]; // append a colon to the getopt string, telling getopt
- buff[0] = ':'; // this option (added to optString in option construct)
- buff[1] = 0; // takes an argument.
- strcat(optString, buff);
- }
-
- char *
- stringOption::getArgument()
- {
- return arg;
- }
-
- int
- stringOption::checkOption(int o, char * a)
- {
- if (o == opt) {
- given = 1;
- arg = a;
- }
-
- return (o == opt);
- }
-
-
- manyArgsOption::manyArgsOption(int o, char * d, char * defaultIn, int n)
- : stringOption(o, d, defaultIn)
- {
- expected = n;
- received = 0;
- args = 0;
- }
-
- char *
- manyArgsOption::getArg(int n)
- {
- if (args == 0)
- parse();
-
- if (n >= received) {
- usage();
- fprintf(stderr, "Error: %d argument(s) requested, only received %d\n",
- n +1, received);
- exit(1);
- }
-
- return args[n];
- }
-
- int
- manyArgsOption::getNumber()
- {
- if (args == 0)
- parse();
-
- return received;
- }
-
- void
- manyArgsOption::parse()
- {
- args = new char *[expected];
-
- if (getArgument() == 0)
- return;
-
- char * scan = strdup(getArgument());
- char * t;
- while (t = strtok(scan, ", ")) {
- if (received == expected) {
- usage();
- fprintf(stderr, "Error: Too many arguments (max is %d)\n",
- expected);
- exit(1);
- }
-
- args[received++] = t;
- scan = 0;
- }
- }
-
-
- commandArgument::commandArgument(char * desc, int n)
- {
- args = 0;
- description = desc;
- expected = n;
- received = 0;
-
- theCommandArgument = this;
- }
-
- int commandArgument::getNumber()
- {
- return received;
- }
-
- char * commandArgument::getArg(int n)
- {
- if (n >= received) {
- fprintf(stderr,
- "%s: Error: %s (argument %d) not specified.\n",
- appName, description, n + 1);
- exit(1);
- }
-
- return args[n];
- }
-
- int commandArgument::setArgument(int n, char ** a)
- {
- if (n > expected) {
- fprintf(stderr, "%s: arguments received is %d, only expecting %d.\n",
- description, n, expected);
- return 0; // return error
- }
-
- received = n;
- args = a;
- return 1; // ok.
- }
-
- void
- commandArgument::usage()
- {
- fprintf(stderr, "%s\n", description);
- }
-
-
- class processOptions : public option
- {
- public:
- processOptions(int, char **);
- };
-
- processOptions::processOptions(int argc, char ** argv)
- : option(0, 0)
- {
- int opt, err = 0;
- option * i;
-
- if ((appName = strrchr(argv[0], '/')) == 0)
- appName = argv[0];
-
- while ((opt = getopt(argc, argv, optString)) != -1)
- if (opt == '?')
- err++;
- else
- {
- int gotten = 0;
-
- for (i = head; i ; i = i->next)
- if (i->checkOption(opt, optarg))
- gotten = 1;
- if (!gotten)
- err++;
- }
-
- if (!err && (theCommandArgument != 0))
- if (theCommandArgument->setArgument(argc - optind, argv + optind) == 0)
- err++;
-
- if (err) {
- fprintf(stderr, "Usage: %s [options] %s\n", appName,
- theCommandArgument ? theCommandArgument->description : "");
- fputs("options:\n", stderr);
-
- for (i = head; i ; i = i->next)
- i->usage();
-
- exit(1);
- }
-
- Main();
- }
-
-
- /*
- * This is the real main that processes all the command line options.
- * the user defined "Main" is called after this.
- */
- int
- main(int argc, char ** argv)
- {
- processOptions p(argc, argv);
- }
-